Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(console): add subcommand for gen completions #336

Merged
merged 14 commits into from
May 23, 2022

Conversation

nrskt
Copy link
Sponsor Contributor

@nrskt nrskt commented Apr 26, 2022

This PR includes adding subcommand for shell completions. Generating a man page will be done as another PR.

❯ tokio-console
--ascii-only            --lang                  --no-duration-colors    --retain-for            -h                      gen-config
--colorterm             --log                   --no-terminated-colors  --version               <TARGET_ADDR>           help
--help                  --no-colors             --palette               -V                      gen-completion

❯ tokio-console gen-completion
--help      --version   -V          -h          bash        elvish      fish        powershell  zsh

Related to #325

@nrskt nrskt requested a review from a team as a code owner April 26, 2022 18:06
Comment on lines 102 to 104
/// It should be saved in expected directory, depending on the shell used
///
/// $ tokio-console gen-completion zsh > $FPATH/_tokio_console
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since we know the shell from the command-line argument, should we just make the command install the completions to the correct location, instead?

Copy link
Sponsor Contributor Author

@nrskt nrskt Apr 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hawkw Maybe it can install the completion but I think that It's better not to do it because the user wants to decide where is put.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, isn't the completion location determined by the shell, rather than the user? i'm fine with the current approach, but think this is worth a potential follow-up.

Comment on lines 39 to 43
if let Some(config::OptionalCmd::GenCompletion { shell }) = args.subcmd {
let mut app = config::Config::command();
generate(shell, &mut app, "tokio-console", &mut std::io::stdout());
return Ok(());
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style, take it or leave it: now that we have multiple subcommands, we might want to consider collapsing this and the if block for the GenConfig subcommand into a match, like

match args.subcmd {
    Some(config::OptionalCmd::GenConfig) => // ...
    Some(config::OptionalCmd::GenCompletion { shell } => // ...
    None => {},
}

Copy link
Sponsor Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed: b51ce10

tokio-console/src/config.rs Show resolved Hide resolved

/// Generate shell completions
///
/// It should be saved in expected directory, depending on the shell used
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe something like this:

Suggested change
/// It should be saved in expected directory, depending on the shell used
/// The completion script should be saved in the shell's completion directory.
/// This depends on which shell is in use.
///
/// For example, using zsh:

Copy link
Sponsor Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed: 60fbce5

Comment on lines 102 to 104
/// It should be saved in expected directory, depending on the shell used
///
/// $ tokio-console gen-completion zsh > $FPATH/_tokio_console
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, isn't the completion location determined by the shell, rather than the user? i'm fine with the current approach, but think this is worth a potential follow-up.

@nrskt
Copy link
Sponsor Contributor Author

nrskt commented May 2, 2022

@hawkw

hmm, isn't the completion location determined by the shell, rather than the user? i'm fine with the current approach, but think this is worth a potential follow-up.

Ok, I understand your opinion. I'll check how to completion directory the shell that I don't know (fish, elvish, powershell).
Should I fix it with another pull request? or continually this pull request?

///
/// For example, using zsh:
///
/// $ tokio-console gen-completion zsh > ~/.zsh_functions/_tokio_console
Copy link
Sponsor Contributor Author

@nrskt nrskt May 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hawkw
Sorry, I'm mistaken.
$FPATH has multiple path values so it's not work $FPATH/_tokio_console

I worried about this sample, because .zsh_functions directory name. This name is my config value on .zshrc. I don't know common name.

fa91ca9

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, if this isn't going to work for everyone, maybe we should remove the sample for now, and add it back later if we can come up with one that will work universally?

Comment on lines 227 to 246
fn gen_completion(install: bool, shell: Shell) -> color_eyre::Result<()> {
let mut app = config::Config::command();
let mut buf: Box<dyn std::io::Write> = if install {
let mut home_dir = dirs::home_dir()
.ok_or_else(|| color_eyre::eyre::eyre!("fail to find home directory"))?;
match shell {
Shell::Zsh => {
home_dir.push(".zsh_functions/_tokio_console");
let f = std::fs::File::create(&home_dir)
.wrap_err_with(|| format!("fail to open {}", home_dir.display()))?;
Box::new(std::io::BufWriter::new(f))
}
_ => color_eyre::eyre::bail!("Not support to install completion script on {}", shell),
}
} else {
Box::new(std::io::stdout())
};
generate(shell, &mut app, "tokio-console", &mut buf);
Ok(())
}
Copy link
Sponsor Contributor Author

@nrskt nrskt May 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hawkw
How about this approach?
If the user runs as tokio-console gen-completion --install <SHELL> then put the script file.

But I must fix it, .zsh_functions directory is not correct. I have a problem.
I have tried to get a directory with $FPATH environment variables however I failed.

problem:
std::env::var("FPATH") return an error. I checked on my terminal like this:

❯ echo $FPATH
/home/nrskt/.zsh_functions:....some paths

❯ env | rg FPATH
(return nothing)

So FPATH not working in my environment. I can't determine if it's a problem with my environment or zsh features.


In my opinion, first I fix it to not support all shells like this:

match shell {
    _ => color_eyre::eyre::bail!("Not support to install completion script on {}", shell),
}

And the rest should be treated for potential follow-up.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm...it seems like automatically installing completions may be a bit more complicated than i'd hoped. i think it's reasonable to just always fail if --install is passed, for now, so we can go ahead and merge this branch, and implement support for automatic installations later.

@nrskt nrskt requested a review from hawkw May 3, 2022 20:12
Copy link
Member

@hawkw hawkw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall, this looks good! I had some small suggestions.

I think it's fine to save the automatic installation of completion scripts for later, since it looks like it's actually somewhat complex. For now, let's go with what you suggested and have that always fail, and the user can still install completions manually.

@@ -214,3 +223,24 @@ async fn watch_details_stream(
}
}
}

fn gen_completion(install: bool, shell: Shell) -> color_eyre::Result<()> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit, take it or leave it: I think it might make sense to put this function in the config module?

Copy link
Sponsor Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed: 99f659f

Comment on lines 227 to 246
fn gen_completion(install: bool, shell: Shell) -> color_eyre::Result<()> {
let mut app = config::Config::command();
let mut buf: Box<dyn std::io::Write> = if install {
let mut home_dir = dirs::home_dir()
.ok_or_else(|| color_eyre::eyre::eyre!("fail to find home directory"))?;
match shell {
Shell::Zsh => {
home_dir.push(".zsh_functions/_tokio_console");
let f = std::fs::File::create(&home_dir)
.wrap_err_with(|| format!("fail to open {}", home_dir.display()))?;
Box::new(std::io::BufWriter::new(f))
}
_ => color_eyre::eyre::bail!("Not support to install completion script on {}", shell),
}
} else {
Box::new(std::io::stdout())
};
generate(shell, &mut app, "tokio-console", &mut buf);
Ok(())
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm...it seems like automatically installing completions may be a bit more complicated than i'd hoped. i think it's reasonable to just always fail if --install is passed, for now, so we can go ahead and merge this branch, and implement support for automatic installations later.

} else {
Box::new(std::io::stdout())
};
generate(shell, &mut app, "tokio-console", &mut buf);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit, take it or leave it: i would probably not import the generate function into this file globally, as it's not super clear what's being generated this way. i would probably have written

Suggested change
generate(shell, &mut app, "tokio-console", &mut buf);
clap_complete::generate(shell, &mut app, "tokio-console", &mut buf);

but, it's not a big deal either way.

Copy link
Sponsor Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed: 0c02ebf

tokio-console/src/main.rs Outdated Show resolved Hide resolved
tokio-console/src/main.rs Outdated Show resolved Hide resolved
.wrap_err_with(|| format!("fail to open {}", home_dir.display()))?;
Box::new(std::io::BufWriter::new(f))
}
_ => color_eyre::eyre::bail!("Not support to install completion script on {}", shell),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe

Suggested change
_ => color_eyre::eyre::bail!("Not support to install completion script on {}", shell),
_ => color_eyre::eyre::bail!("Automatically installing completion scripts is not currently supported on {}", shell),

///
/// For example, using zsh:
///
/// $ tokio-console gen-completion zsh > ~/.zsh_functions/_tokio_console
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, if this isn't going to work for everyone, maybe we should remove the sample for now, and add it back later if we can come up with one that will work universally?

@nrskt
Copy link
Sponsor Contributor Author

nrskt commented May 7, 2022

@hawkw Fixed based on your suggestion. How about it?

@nrskt nrskt requested a review from hawkw May 7, 2022 06:32
Copy link
Member

@hawkw hawkw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay, this looks good to me! thanks!

#[clap(name = "install", long = "install")]
install: bool,
#[clap(arg_enum)]
shell: Shell,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i wonder if it's worthwhile to also try to read this from the value of the $SHELL environment variable, if it's not provided by the user? we could add that in a follow-up.

@hawkw hawkw enabled auto-merge (squash) May 23, 2022 21:11
hawkw added 3 commits May 23, 2022 14:11
<a name="0.1.6"></a>
## 0.1.6 (2022-05-23)

#### Features

*  add `Builder::poll_duration_histogram_max` (tokio-rs#351) ([a966feb](a966feb))

#### Bug Fixes

*  fix memory leak from resizing histograms (tokio-rs#351) ([32dd337](32dd337), closes [tokio-rs#350](350))
@hawkw hawkw merged commit df4d468 into tokio-rs:main May 23, 2022
hawkw added a commit that referenced this pull request May 24, 2022
<a name="0.1.6"></a>
## 0.1.6 (2022-05-24)

#### Bug Fixes

*  default `--no_colors` to `false` (#344) ([e58352f](e58352f))

#### Features

*  add subcommand to gen shell completions (#336) ([df4d468](df4d468))
*  display outliers in histogram view (#351) ([dec891f](dec891f))
hawkw added a commit that referenced this pull request May 24, 2022
<a name="0.1.6"></a>
## 0.1.6 (2022-05-24)

#### Bug Fixes

*  default `--no_colors` to `false` (#344) ([e58352f](e58352f))

#### Features

*  add subcommand to gen shell completions (#336) ([df4d468](df4d468))
*  display outliers in histogram view (#351) ([dec891f](dec891f))
hawkw added a commit that referenced this pull request May 24, 2022
<a name="0.1.6"></a>
## 0.1.6 (2022-05-24)

#### Bug Fixes

*  default `--no_colors` to `false` (#344) ([e58352f](e58352f))

#### Features

*  add subcommand to gen shell completions (#336) ([df4d468](df4d468))
*  display outliers in histogram view (#351) ([dec891f](dec891f))
hawkw added a commit that referenced this pull request May 24, 2022
<a name="0.1.6"></a>
## 0.1.6 (2022-05-24)

#### Bug Fixes

*  default `--no_colors` to `false` (#344) ([e58352f](e58352f))

#### Features

*  add subcommand to gen shell completions (#336) ([df4d468](df4d468))
*  display outliers in histogram view (#351) ([dec891f](dec891f))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants